package com.xindaoapp.henghua.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.xindaoapp.henghua.R;
import com.xindaoapp.henghua.application.XinDaoApplication;
import com.xindaoapp.henghua.asset.DeviceAdapter;
import com.xindaoapp.henghua.asset.DeviceWindow;
import com.xindaoapp.henghua.asset.ILoadDatas;
import com.xindaoapp.henghua.entry.PropertyGatherInfo;
import com.xindaoapp.henghua.utils.UpdateDataChanged.OnUpdateDataChangedListener;
import com.xindaoapp.henghua.view.CustomViewPager;

/**
 * 设备监造列表页面(主页面)
 * 
 * @author chenghao
 * 
 */
public class DeviceEngineerActivity extends BaseActivity {
	private CustomViewPager mViewPager;
	private EditText search;
	/** ViewPager 包含的3个view */
	private DeviceWindow[] mDeviceWindows;
	private TextView[] mTabs;
	private TextView tvTitle;

	public enum PASTETYPE {
		UNPASTED, ALL, PASTED
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_device_enginner);
		initViews();
		XinDaoApplication.get().getUpdateDataChanged().add(mOnUpdateDataChangedListener);

		// 判断是否有未提交数据，有则跳转到未提交界面
		if (isHaveUnSubmitLabel()) {
			Intent intent = new Intent(getSelf(), UnSubmitActivity.class);
			startActivity(intent);
		}
	}

	private void initViews() {
		search = (EditText) findViewById(R.id.search);
		search.addTextChangedListener(mTextWatcher);
		mViewPager = (CustomViewPager) findViewById(R.id.pager);
		mTabs = new TextView[] { (TextView) findViewById(R.id.tab_left),
				(TextView) findViewById(R.id.tab_center), (TextView) findViewById(R.id.tab_right) };
		mViewPager.initTabs(mTabs, 1);
		DeviceWindow leftDeviceWindow = new DeviceWindow(this, PASTETYPE.UNPASTED.ordinal(), iLoadDatas);
		DeviceWindow centerDeviceWindow = new DeviceWindow(this, PASTETYPE.ALL.ordinal(), iLoadDatas);
		DeviceWindow rightDeviceWindow = new DeviceWindow(this, PASTETYPE.PASTED.ordinal(), iLoadDatas);
		mDeviceWindows = new DeviceWindow[] { leftDeviceWindow, centerDeviceWindow, rightDeviceWindow };
		// 对页面进行初始化
		for (int i = 0; i < mDeviceWindows.length; i++) {
			mDeviceWindows[i].getView();
		}
		mViewPager.setAdapter(new DeviceAdapter(mDeviceWindows));
		mViewPager.setCurrentItem(1);
		mViewPager.setOnPageChangeListener(mOnPageChangeListener);
		mDeviceWindows[1].initPropertyInfos();

		tvTitle = getTvTitle();
	}

	@Override
	protected void onStart() {
		super.onStart();
		tvTitle.setText(XinDaoApplication.get().getUserInfo().project_code + "-物资监造");
	}

	private final ILoadDatas iLoadDatas = new ILoadDatas() {

		@Override
		public void refresh(List<PropertyGatherInfo> list) {
			reload();
		}

		@Override
		public void reloadHomePage(List<PropertyGatherInfo> list) {
			XinDaoApplication.get().getUpdateDataChanged().setPropertyGatherInfos(sort(list));
			list = XinDaoApplication.get().getUpdateDataChanged().getPropertyGatherInfos();
			mDeviceWindows[1].reload(list);
			reload();
		}

		private void reload() {
			mDeviceWindows[0].reload(getPasted(true));
			mDeviceWindows[2].reload(getPasted(false));
			for (int i = 0; i < PASTETYPE.values().length; i++) {
				if (i == PASTETYPE.UNPASTED.ordinal()) {
					mTabs[0].setText(String.format(getString(R.string.unpasted),
							mDeviceWindows[i].getDataSize()));
				} else if (i == PASTETYPE.ALL.ordinal()) {
					mTabs[1].setText(String.format(getString(R.string.all), mDeviceWindows[i].getDataSize()));
				} else if (i == PASTETYPE.PASTED.ordinal()) {
					mTabs[2].setText(String.format(getString(R.string.pasted),
							mDeviceWindows[i].getDataSize()));
				}
			}
		}

	};

	private List<PropertyGatherInfo> sort(List<PropertyGatherInfo> list) {
		List<PropertyGatherInfo> labeled = new ArrayList<PropertyGatherInfo>();
		List<PropertyGatherInfo> noLabeled = new ArrayList<PropertyGatherInfo>();
		for (PropertyGatherInfo info : list) {
			if (isHasLabel(info)) {
				labeled.add(info);
			} else {
				noLabeled.add(info);
			}
		}
//		sortAllDatas(labeled);
		sortAllDatas(noLabeled);
		List<PropertyGatherInfo> temp = new ArrayList<PropertyGatherInfo>();
		temp.addAll(noLabeled);
		temp.addAll(labeled);
		return temp;
	}

	/**
	 * 过滤数据
	 * 
	 * @param isUnPasted
	 * @return
	 */
	private List<PropertyGatherInfo> getPasted(boolean isUnPasted) {
		List<PropertyGatherInfo> temp = new ArrayList<PropertyGatherInfo>();
		if (XinDaoApplication.get().getUpdateDataChanged().getPropertyGatherInfos() == null) {
			return temp;
		}
		for (PropertyGatherInfo info : XinDaoApplication.get().getUpdateDataChanged()
				.getPropertyGatherInfos()) {
			if (isUnPasted) {
				if (!isHasLabel(info)) {
					temp.add(info);
				}
			} else {
				if (isHasLabel(info)) {
					temp.add(info);
				}
			}
		}

		// 对数据进行排序
		sortAllDatas(temp);
		return temp;
	}

	public static boolean isHasLabel(PropertyGatherInfo info) {
		if (info.propertyLabelInfos != null) {
			if (info.propertyLabelInfos.size() > 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 对数据进行排序
	 * @param list
	 */
	private void sortAllDatas(List<PropertyGatherInfo> list) {
		Collections.sort(list, new Comparator<PropertyGatherInfo>() {

			@Override
			public int compare(PropertyGatherInfo lhs, PropertyGatherInfo rhs) {
				return lhs.code.compareTo(rhs.code);
			}
		});
	}

	private final OnUpdateDataChangedListener mOnUpdateDataChangedListener = new OnUpdateDataChangedListener() {

		@Override
		public void onUpdateDataChanged() {
			mDeviceWindows[1].refursh();
			/*mDeviceWindows[1].reload(XinDaoApplication.get().getUpdateDataChanged().getPropertyGatherInfos());
			mDeviceWindows[0].reload(getPasted(true));
			mDeviceWindows[2].reload(getPasted(false));*/
		}
	};

	private final TextWatcher mTextWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			mDeviceWindows[mViewPager.getCurrentItem()].textChanged(s.toString());
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};

	private final OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int position) {
			hideInputMethod();
			mViewPager.selected(position);
			if (position == 0) {
				mDeviceWindows[0].reload(getPasted(true));
			} else if (position == 1) {
				mDeviceWindows[2].reload(getPasted(false));
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	};

	@Override
	protected boolean onShowBackButton() {
		return false;
	}

	@Override
	protected String getTabTitle() {
		return "110kv变电工程-物资监造";
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		XinDaoApplication.get().getUpdateDataChanged().remove(mOnUpdateDataChangedListener);
	}

	private void hideInputMethod() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromInputMethod(search.getWindowToken(), 0);
	}
}
